/**
 * Created by gongxun on 16/12/19.
 */

var KwxDialogChat = GameCCSBase.extend({
    Button_chat : null,
    Image_main : null,

    //快捷短语
    Panel_bubble : null,
    Image_selectText : null,
    Image_selectFace : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxTableChat.json");

        this.Image_main = mo.seekWidgetByName(this.ccsNode, "Image_main");
        this.Panel_bubble = mo.seekWidgetByName(this.ccsNode, "Panel_bubble");
        this.Image_selectText = mo.seekWidgetByName(this.ccsNode, "Image_selectText");
        this.Image_selectFace = mo.seekWidgetByName(this.ccsNode, "Image_selectFace");

        //button
        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.setDialog(false);
            }
        }, this);
        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.setDialog(false);
            }
        }, this);

        this.loadText();

        this.loadFace();

        this.setVisible(false);

        this.Panel_bubble.setVisible(false);
    },

    onEnter : function(){
        this._super();

        this.updateText();
        this.updateFace();

        mo.eventHelper.add("updateUserCostomRate", this, function(){
            this.updateText();
            this.updateFace();
        });
    },

    onExit : function(){
        this._super();

        mo.eventHelper.remove(this);
    },

    setDialog : function(isVisible){
        this.setVisible(isVisible);
    },

    sendChatMsg : function(index){
        mo.gameMsgManager.sendUserChatMsg(mo.gameUserInfo.uid, index.toString(), 1);

        if (this.Button_chat != null){
            this.Button_chat.stopAllActions();
            this.Button_chat.setEnabled(false);
            var that = this;
            this.Button_chat.runAction(cc.sequence(
                cc.delayTime(3.5),
                cc.callFunc(function(){
                    that.Button_chat.setEnabled(true);
                })
            ));
        }
    },

    loadText : function(){
        var that = this;
        for (var i = 0; i < 13; i++){
            var Image_textSelect = mo.seekWidgetByName(this.ccsNode, "Image_textSelect" + i);
            mo.gameHelper.setRootTitleText(Image_textSelect, mo.gameUserInfo.userCustomText[i]);
            Image_textSelect.setTag(i);

            Image_textSelect.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)){
                    that.setTextSelect(sender, false);

                    //发送
                    var tag = sender.getTag();
                    var index = mo.gameUserInfo.userCustomTextRateStruct[tag].index;
                    cc.log("index:" + index);
                    that.sendChatMsg(index);
                    this.setDialog(false);

                    //更新本地计数
                    mo.gameUserInfo.userCustomTextRate[index]++;
                    mo.gameUserInfo.updateUserCostomRate();

                }else if (type == ccui.Widget.TOUCH_BEGAN){
                    that.setTextSelect(sender, true);
                }else if (type == ccui.Widget.TOUCH_CANCELED){
                    that.setTextSelect(sender, false);
                }
            }, this);
        }
    },

    setTextSelect : function(root, isSelect){
        var Text_text = mo.seekWidgetByName(root, "Text_text");
        Text_text.setTextColor(!isSelect ? cc.color(245, 226, 190) : cc.color(198, 178, 92));
    },

    loadFace : function(){
        var that = this;
        var imageFaceCount = 27;
        var rootSize = cc.size(360, Math.ceil(imageFaceCount / 3) * 80);
        var itemSize = cc.size(120, 80);
        var ScrollView_face = mo.seekWidgetByName(this.ccsNode, "ScrollView_face");
        ScrollView_face.setInnerContainerSize(rootSize);

        for (var i = 0; i < imageFaceCount; i++){
            var Image_image = mo.seekWidgetByName(this.ccsNode, "Image_image" + i);
            Image_image.loadTexture("emoji_index_" + i + ".png", ccui.Widget.PLIST_TEXTURE);
            Image_image.setTag(i);

            //排序
            var x = itemSize.width / 2 + i % 3 * itemSize.width;
            var y = rootSize.height - itemSize.height / 2 - parseInt(i / 3) * itemSize.height;
            Image_image.setPosition(cc.p(x, y));

            //处理
            Image_image.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)){
                    that.setFaceSelect(sender, false);

                    //发送
                    var tag = sender.getTag();
                    var index = mo.gameUserInfo.userCostomFaceRateStruct[tag].index;
                    cc.log("index:" + index);
                    that.sendChatMsg(index + 51);
                    this.setDialog(false);

                    //更新本地计数
                    mo.gameUserInfo.userCostomFaceRate[index]++;
                    mo.gameUserInfo.updateUserCostomRate();

                }else if (type == ccui.Widget.TOUCH_BEGAN){
                    that.setFaceSelect(sender, true);
                }else if (type == ccui.Widget.TOUCH_CANCELED){
                    that.setFaceSelect(sender, false);
                }
            }, this);
        }
    },

    setFaceSelect : function(root, isSelect){
        root.setScale(isSelect ? 1 : 0.8);
    },

    updateText : function(){
        for (var i = 0; i < 13; i++) {
            var Image_textSelect = mo.seekWidgetByName(this.ccsNode, "Image_textSelect" + i);
            var index = mo.gameUserInfo.userCustomTextRateStruct[i].index;
            mo.gameHelper.setRootTitleText(Image_textSelect, mo.gameUserInfo.userCustomText[index]);
        }
    },

    updateFace : function(){
        for (var i = 0; i < 27; i++) {
            var Image_image = mo.seekWidgetByName(this.ccsNode, "Image_image" + i);
            var index = mo.gameUserInfo.userCostomFaceRateStruct[i].index;
            Image_image.loadTexture("emoji_index_" + index + ".png", ccui.Widget.PLIST_TEXTURE);
        }
    },

    //设置聊天按钮和打开面板的回调
    setChatButton : function(btnChat, gameName){
        this.Button_chat = btnChat;
        var Image_bubbleBg = mo.seekWidgetByName(this.ccsNode, "Image_bubbleBg");

        this.setBubblePoint(gameName);

        var bubbleBgPos = cc.p(
            Image_bubbleBg.getPosition().x - Image_bubbleBg.getContentSize().width / 2,
            Image_bubbleBg.getPosition().y - Image_bubbleBg.getContentSize().height / 2
        );

        //加载位置
        var textRects = [];
        var faceRects = [];

        for (var i = 0; i < 5; i++){
            var item = mo.seekWidgetByName(this.Panel_bubble, "Text_content" + i);
            var pos = item.getPosition();
            var rect = cc.rect(pos.x - 5, pos.y - 19.5, 225, 39);
            textRects.push(rect);
        }

        for (var i = 0; i < 3; i++){
            var item = mo.seekWidgetByName(this.Panel_bubble, "Image_face" + i);
            var pos = item.getPosition();
            var rect = cc.rect(pos.x - 41, pos.y - 41.5, 82, 83);
            faceRects.push(rect);
        }

        this.Button_chat.addTouchEventListener(function(sender, type){
            if (type == ccui.Widget.TOUCH_BEGAN){
                this.showBubbleLayer();
            }else if (type == ccui.Widget.TOUCH_MOVED){

                //移动过程中
                var location = sender.getTouchMovePosition();
                var textIndex = -1;
                var faceIndex = -1;
                location.x -= bubbleBgPos.x;
                location.y -= bubbleBgPos.y;
                // cc.log("test chat x:" + location.x + " y:" + location.y);

                for (var i = 0; i < 5; i++){
                    if (cc.rectContainsPoint(textRects[i], location)){
                        textIndex = i;
                        break;
                    }
                }

                for (var i = 0; i < 3; i++){
                    if (cc.rectContainsPoint(faceRects[i], location)){
                        faceIndex = i;
                        break;
                    }
                }

                //设置强调
                this.setBubbleLayerSelect(textIndex, faceIndex);

            }else if (type == ccui.Widget.TOUCH_CANCELED){

                //抬起
                var location = sender.getTouchEndPosition();
                var textIndex = -1;
                var faceIndex = -1;
                location.x -= bubbleBgPos.x;
                location.y -= bubbleBgPos.y;
                // cc.log("test chat end x:" + location.x + " y:" + location.y);

                for (var i = 0; i < 5; i++){
                    if (cc.rectContainsPoint(textRects[i], location)){
                        textIndex = i;
                        break;
                    }
                }

                for (var i = 0; i < 3; i++){
                    if (cc.rectContainsPoint(faceRects[i], location)){
                        faceIndex = i;
                        break;
                    }
                }

                //发送
                this.sendBubbleLayerSelect(textIndex, faceIndex);

                this.setDialog(false);

            }else if (mo.gameHelper.getIsTouchEnd(type)) {

                this.setDialog(true);
                this.Image_main.setVisible(true);
                this.Panel_bubble.setVisible(false);
            }
        }, this);
    },

    //显示快捷短语界面
    showBubbleLayer : function(){
        this.setDialog(true);
        this.Image_main.setVisible(false);
        this.Panel_bubble.setVisible(true);

        this.Image_selectText.setVisible(false);
        this.Image_selectFace.setVisible(false);

        //赋值
        for (var i = 0; i < 5; i++){
            var item = mo.seekWidgetByName(this.Panel_bubble, "Text_content" + i);

            var index = mo.gameUserInfo.userCustomTextRateStruct[i].index;
            mo.gameHelper.setTextString(item, mo.gameHelper.getUserNickFilterForCount(mo.gameUserInfo.userCustomText[index], 10));
            item.setTag(index);
        }

        for (var i = 0; i < 3; i++){
            var item = mo.seekWidgetByName(this.Panel_bubble, "Image_face" + i);
            var index = mo.gameUserInfo.userCostomFaceRateStruct[i].index;
            item.loadTexture("emoji_index_" + index + ".png", ccui.Widget.PLIST_TEXTURE);
            item.setTag(index);
        }
    },

    //设置快捷短语选中状态
    setBubbleLayerSelect : function(textIndex, faceIndex){
        if (textIndex != -1) {
            this.Image_selectText.setVisible(true);
            this.Image_selectFace.setVisible(false);

            var item = mo.seekWidgetByName(this.Panel_bubble, "Text_content" + textIndex);
            var pos = item.getPosition();
            this.Image_selectText.setPosition(cc.p(pos.x - 5, pos.y));

        }else if (faceIndex != -1){
            this.Image_selectText.setVisible(false);
            this.Image_selectFace.setVisible(true);

            var item = mo.seekWidgetByName(this.Panel_bubble, "Image_face" + faceIndex);
            var pos = item.getPosition();
            this.Image_selectFace.setPosition(pos);
        }else{
            this.Image_selectText.setVisible(false);
            this.Image_selectFace.setVisible(false);

        }
    },

    //发送选中的快捷短语
    sendBubbleLayerSelect : function (textIndex, faceIndex) {
        if (textIndex != -1) {
            var item = mo.seekWidgetByName(this.Panel_bubble, "Text_content" + textIndex);
            var index = item.getTag();
            this.sendChatMsg(index);
            this.setDialog(false);

            //更新本地计数
            mo.gameUserInfo.userCustomTextRate[index]++;
            mo.gameUserInfo.updateUserCostomRate();

        }else if (faceIndex != -1){

            var item = mo.seekWidgetByName(this.Panel_bubble, "Image_face" + faceIndex);
            var index = item.getTag();
            this.sendChatMsg(index + 51);
            this.setDialog(false);

            //更新本地计数
            mo.gameUserInfo.userCostomFaceRate[index]++;
            mo.gameUserInfo.updateUserCostomRate();
        }
    },

    //调整背景位置
    setBubblePoint : function(gameName){
        var Image_bubbleBg = mo.seekWidgetByName(this.Panel_bubble, "Image_bubbleBg");
        //背景位置
        if (gameName == "kwx"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, mo.gameSize.y - 300));
        }else if (gameName == "nn"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY()));
        }else if (gameName == "psz"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY() + 10));
        }else if (gameName == "gsbdx"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY() + 20));
        }else if (gameName == "dzpk"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen2").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(140, 250));
        }else if (gameName == "dtj"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY()));
        }else if (gameName == "ddz"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY()));
        }else if (gameName == "pdk"){
            mo.seekWidgetByName(this.Panel_bubble, "Image_zhizhen1").setVisible(true);
            Image_bubbleBg.setPosition(cc.p(mo.gameSize.x - 250, this.Button_chat.getPositionY()));
        }
    }
});